package cc.edge2.sns.services;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.jdo.PersistenceManager;

import net.arnx.jsonic.JSON;
import cc.edge2.sns.commons.PMF;
import cc.edge2.sns.models.Diary;
import cc.edge2.sns.models.SNSUser;

public class GetDiaryService {
	public String execute(Map params, SNSUser user) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		long diaryId = Integer.parseInt((String)params.get("diaryId"));
		Diary diary = pm.getObjectById(Diary.class, diaryId);
		diary = pm.detachCopy(diary);
		
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> contents = new HashMap<String, Object>();
		Map<String, Object> status = new HashMap<String, Object>();
		status.put("code", "0");
		status.put("message", "success");
		contents.put("subject", diary.getSubject());
		contents.put("body", diary.getBody());
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		contents.put("publishedDate", sdf1.format(diary.getPublishedDate()));
		result.put("status", status);
		result.put("contents", contents);
		String value = JSON.encode(result);
		return value;

	}
}
